#!/bin/bash
for fileName in `find ./ -name *.jar`
do
    APPLICATION_JAR=${fileName##*/}
done

ps -ef |grep ${APPLICATION_JAR}$ |awk '{print $2}'|xargs -tr kill -9
if [ ! -x "./log" ]; then
        echo "log is not exist"
        mkdir log
fi

folder_name=`stat nohup.out | grep 'Access' |  awk '{ if (NR > 1 ) { print $2}}' | sed "s/-/_/g"`
time_format=`stat nohup.out | grep "Access" |  awk '{ if (NR > 1 ) { print $2,$3}}'`
timestamp=`date -d "$time_format" +%s%3N`
echo timestamp

if [ ! -x "./log/${folder_name}" ]; then
        echo "date log not exist"
        mkdir ./log/${folder_name}
fi
echo "bak nohup.out to ./log/${folder_name}"
if [ ! -x "./log/`date +%Y_%m_%d`" ]; then
      echo "date log not exist"
      mkdir ./log/`date +%Y_%m_%d`
fi

mv nohup.out log/${folder_name}/nohup_bak_${timestamp}.out

#==========================================================================================
# JVM Configuration
# -Xmx1g:设置JVM最大可用内存为1G。
# -Xms1g:设置JVM初始内存1g。此值可以设置与-Xmx相同,以避免每次垃圾回收完成后JVM重新分配内存
# -Xmn512m:设置年轻代大小为512m。整个JVM内存大小=年轻代大小 + 年老代大小 + 持久代大小。
#          持久代一般固定大小为64m,所以增大年轻代,将会减小年老代大小。此值对系统性能影响较大,Sun官方推荐配置为整个堆的3/8
# -XX:MetaspaceSize=64m:存储class的内存大小,该值越大触发Metaspace GC的时机就越晚
# -XX:MaxMetaspaceSize=256m:限制Metaspace增长的上限，防止因为某些情况导致Metaspace无限的使用本地内存，影响到其他程序
# -XX:-OmitStackTraceInFastThrow:解决重复异常不打印堆栈信息问题
#==========================================================================================
#测试使用资源较小的启动参数,生产根据服务器配置适当调整资源配置
#JAVA_OPT="-server -Xms1g -Xmx1g -Xmn512m -XX:MetaspaceSize=64m -XX:MaxMetaspaceSize=256m"
JAVA_OPT="-server -Xms64m -Xmx64m -Xmn32m -XX:MetaspaceSize=32m -XX:MaxMetaspaceSize=128m"
JAVA_OPT="${JAVA_OPT} -XX:-OmitStackTraceInFastThrow"

nohup java ${JAVA_OPT} -jar ${APPLICATION_JAR} >> nohup.out 2>&1 &
if [ -n "$1" ]; then
    tail -F nohup.out
else
    echo "run $0 success"
fi
